package util;

import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Properties;

/**
 * @author gloria
 */
public enum ImageUtil {
    /**
     * ImageUtil's singleton instance.
     */
    INSTANCE;

    private String imagePath;

    ImageUtil() {
        String path = "uploadPath";
        Properties properties = new Properties();
        try {
            properties.load(ImageUtil.class.getResourceAsStream("/conf/config.properties"));
            imagePath = properties.getProperty(path);
            if (imagePath == null) {
                throw new NoSuchPropertyException("Not found property[" + path + "].");
            }
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }

    /**
     * @param subfix 图片名后需要加的后缀，例如jsp等
     * @return 返回的图片名为当前时间+后缀
     */
    public static String getImageName(String subfix) {
        Date date = new Date();
        SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddhhmmss");
        String imageName = sdf.format(date);
        return imageName + "." + subfix;
    }

    public String getImagePath() {
        return imagePath;
    }
}
